<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/2
 * Time: 15:51
 */
namespace app\api\controller;
use think\Db;
class  Bank extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $useridddd = input('user_id');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($useridddd,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    //列表
    public function index(){
        $page = input('param.page')?input('param.page'):1;
        $subject_id = input('param.subject_id');
        $num = 10;
        $limit = $page-1;
        $where['question_subject_id'] = $subject_id;
        $where['question_status'] = 1;
        // 用户年级条件
        $user_id = input('user_id');
        $user_info = Db::table('up_u_user')
            ->where('user_id', $user_id)
            ->field('user_grade')
            ->find();
        $small = [1,2,3,4,5,6];
        $middle = [7,8,9];
        $hight = [10,11,12];
        if(in_array($user_info['user_grade'], $small)){
            $where['question_grade_id'] = ['IN', $small];
        }
        if(in_array($user_info['user_grade'], $middle)){
            $where['question_grade_id'] = ['IN', $middle];
        }
        if(in_array($user_info['user_grade'], $hight)){
            $where['question_grade_id'] = ['IN', $hight];
        }
        $res = Db::table('up_question_bank')->where($where)
            ->limit($num*$limit,$num)
            ->order('question_is_top asc, question_time desc')
            ->select();
        foreach ($res as $key=>&$vo){
            if($vo['question_from']==1){
                $vo['user_image']=Db::table('up_u_user')->where('user_id',$vo['question_user_id'])->value('user_image');
            }else{
                $vo['user_image'] ='';
            }
        }
        return json(['status' => 200, 'msg' => $res]);
    }
    //详情
    public function detail(){
        extract(input());
        $id = input('param.id');
        $where['question_id'] = $id;
        $res = Db::table('up_question_bank')->where($where)
            ->find();
        if($res['question_type']==1){
            $res['is_money'] = 1;//免费
        }else{
            $map['purchase_type'] = 2;
            $map['f_id'] = $id;
            $map['purchase_user_id'] = $user_id;
            $data = Db::table('up_purchase')->where($map)->find();
            if($data['purchase_id']!=''){
                $res['is_money'] = 2;//收费
             }else{
                $res['is_money'] = 1;//免费
            }
        }

        $info = Db::table('up_question_file')->where('q_question_id',$id)->select();
        $res['info'] = $info;
        return json(['status' => 200, 'msg' => $res]);
    }

    //购买接口
    public function buy(){
        extract(input());
        $id = input('param.id');//题库ID
        $money = input('param.money');
        $purchase_target = input('param.purchase_target');
        $user_balance = Db::table('up_u_user')->where('user_id',$user_id)->value('user_balance');
        if($money>$user_balance){
            return  json(array('status'=>100,'msg'=>"平台币不足,请先充值"));
        }else{
            Db::table('up_u_user')->where('user_id',$user_id)->setDec('user_balance',$money);
        }

        //插入订单表--学生订单
        $data1['order_type'] = 6;
        $data1['order_price'] = $money;
        $data1['order_time'] = date('Y-m-d H:i:s',time());
        $data1['order_user_id'] = $user_id;
        Db::table('up_order')->insertGetId($data1);
        //插入订单表--老师订单
        $data2['order_type'] = 6;
        $data2['order_price'] = $money;
        $data2['order_time'] = date('Y-m-d H:i:s',time());
        $data2['order_user_id'] = input('param.teacher_id');
        $data2['order_up_down'] = 1;
        Db::table('up_order')->insertGetId($data2);
        //插入购买记录表
        $info1 = Db::table('up_question_bank')->where('question_id',$id)->find();
        if($info1['question_from']==1){
            $data3['teacher_id'] = $info1['question_user_id'];
        }
        $data3['purchase_mony'] = $money;
        $data3['purchase_user_id'] = $user_id;
        $data3['purchase_car_type'] = 1;
        $data3['purchase_type'] = 2;
        $data3['f_id'] = $id;
        $data3['purchase_target'] = $purchase_target;
        $data3['purchase_time'] = date('Y-m-d H:i:s',time());
        Db::table('up_purchase')->insertGetId($data3);
        
        $bank = Db::table('up_question_bank')
            ->where('question_id', $id)
            ->field('question_from, question_user_id')
            ->find();
        
        if($bank['question_from'] == 1){ // 老师发布的题库
            // 老师账户余额增加
            Db::table('up_u_user')->where('user_id', $bank['question_user_id'])->setInc('user_balance', $money);
            // 消息表
            allInNotice('题库购买', '有学生购买了您的题库，快来看看~', 2, 2, 5, $id, $bank['question_user_id']);
            // 极光推送
            $list = [];
            $val = (string)$bank['question_user_id'];
            array_push($list, $val);
            try{
                $this->JpushT(1, $list, '题库购买', ['question_id' => $id, 'send_type' => 2]);
            }catch(\Exception $e){
        
            }
        }
        return  json(array('status'=>200,'msg'=>"购买成功"));

    }
}